package controllers;

import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.HashMap;

import model.ButtonRequestModel;
import model.CustomerHistoryModel;
import view.CustomerHistoryGUI;
import view.FeedbackHistoryGUI;
import view.PurchaseHistoryGUI;
import client.CliMessage;

	
	/**
	 * The Class CustomerHistoryController.
	 * controls the customer History ,extends AbstractController.
	 * @author Tal
	 */
	public class CustomerHistoryController extends AbstractController
	{
		
		
		private CustomerHistoryGUI cusHistoryView;
		
	
		private CustomerHistoryModel cusHistoryModel;
		private ArrayList<CustomerHistoryModel> cusHistoryList;
		
		public CustomerHistoryController(CustomerHistoryGUI cusHistoryView,CustomerHistoryModel cusHistoryModel) 
		{
			super(cusHistoryView,cusHistoryModel);
			this.cusHistoryView = cusHistoryView;
			this.cusHistoryModel = cusHistoryModel;
		
			
		}
		/**
		 * Inner class that handled when Customer Feedback History Button Pressed,extends AbstractListener
		 * @author Tal
		 */
	
		class ViewFeedbackListener extends AbstractListener
		{	
			CustomerHistoryController prevObserver;
			
			public ViewFeedbackListener(CustomerHistoryController prev)
			{
				prevObserver=prev;
			}
		 
			@Override
			public void actionPerformed(ActionEvent e) 
			{
			
				System.out.println("view feedback butto:"+e.getActionCommand());
				
                      int index=Integer.valueOf(e.getActionCommand());
				
				System.out.println("user id feed"+cusHistoryList.get(index).getUID());
				
				
			     int curruid=cusHistoryList.get(index).getUID();         
			     
				//create a new Feedback History controller
				FeedbackHistoryGUI feedHisView = new FeedbackHistoryGUI();
				ButtonRequestModel reqModel = new ButtonRequestModel();
				FeedbackHistoryController feedHisController = new FeedbackHistoryController(feedHisView,reqModel);
				
				//add observer to new controller and delete the rest
				setCurrentObserver(feedHisController);						
				
				//add a listener to "back" button in Feedback history window
				setBackListener(prevObserver,feedHisView);
			

				String searchQuery = "search"	+"SELECT * "
						                        +" FROM customer_complaints AS c "
                                                +" WHERE c.uid="+ curruid +" ";

				sendToServer(searchQuery);
			}
		}
		/**
		 * Inner class that handled when Customer Purchase History Button Pressed,extends AbstractListener
		 * @author Tal
		 */
		
	
		class ViewPurchaseListener extends AbstractListener
		{
              CustomerHistoryController prevObserver;
			
			public ViewPurchaseListener(CustomerHistoryController prev)
			{
				prevObserver=prev;
			}
		
			@Override
			public void actionPerformed(ActionEvent e) 
			{
				System.out.println("view purchase button :"+e.getActionCommand());
				int index=Integer.valueOf(e.getActionCommand());
				
				System.out.println("user id pur"+cusHistoryList.get(index).getUID());
				
			    int curruid=cusHistoryList.get(index).getUID();       
			    
				//create a new Purchase History controller
				PurchaseHistoryGUI purHisView = new PurchaseHistoryGUI();
				
				PurchaseHistoryController purHisController = new PurchaseHistoryController(purHisView);
				
				//add observer to new controller and delete the rest
				setCurrentObserver(purHisController);						
				
				//add a listener to "back" button in puchase history window
				setBackListener(prevObserver,purHisView);
			

				String searchQuery = "search"	+"SELECT * "
						                        +" FROM orders AS o, customer_orders AS c,payment_method AS p"
                                                +" WHERE c.uid="+ curruid +"  AND c.invoice_number=o.invoice_number AND o.pid=p.pid" ;

				sendToServer(searchQuery);
			}
		}
			

		
		@Override
		public void handleDBRequestSuccess() 
		{
			
			
		}

		
		@Override
		public void handleNullSearchResult() 
		{
			System.out.println("inform user: no history!");
			cusHistoryView.displayInfoMessage("There are no Customer History matching serach information."
											,"History Not Found", 1);
		}


		
		
		@Override
		public void handleDBSearchResult(CliMessage dbResult) 
		{
			//get list of Customers
			 cusHistoryList = new ArrayList<CustomerHistoryModel>();
			srs = dbResult.getSrs();
			int nRows = srs.size();
			int clm=3;
			//create data table
			 Object[][] data=new Object[nRows][clm];
		   	
			 String[] columnNames={"User Id", "First Name", "Last Name"};

			//get all tuples
			for(int i = 0 ; i < nRows ; i++)
			{
				// get a tuple
				HashMap<String, String> photoTuple = srs.get(i);
				
				//deatils
				int uid = Integer.parseInt(photoTuple.get("UID"));
				String fname = photoTuple.get("FNAME");
				String lname = photoTuple.get("LNAME");
				
				data[i][0]=uid;
				data[i][1]=fname;
				data[i][2]=lname;
				
				//set a customer detail class
				CustomerHistoryModel cusDetails = new CustomerHistoryModel(uid,fname,lname) ;
				
				//add the photo details to the list
				cusHistoryList.add(cusDetails);
				
				//display for developer
				System.out.println( uid+" "+fname+" "+lname);
				
			}
			
			cusHistoryView.setTable(columnNames, data);
			
			
			
			//set buttons according action
			String[] buttonNames;
		
				buttonNames = new String[2];
				buttonNames[0] = "View Feedback";
				buttonNames[1] = "View Purchase";
			
			
			//create all the buttons and save them to the model
				cusHistoryView.setAllbuttons(nRows, buttonNames);
		
				setActionListeners(buttonNames,nRows);
			
				cusHistoryView.setVisible(true);
		
		}
	
		
		/**
		 * Sets the action listeners.
		 */
		private void setActionListeners(String[] btnNames,int size) 
		{
			HashMap<AbstractListener,AbstractListener> l;
			HashMap <String,AbstractListener> listeners = new HashMap <String,AbstractListener>();
			listeners.put("View Feedback",new ViewFeedbackListener(this));
			listeners.put("View Purchase",new ViewPurchaseListener(this));
			
			String [] buttonNames = btnNames ;
			int nRows = size;
			
			for (int i = 0; i < nRows; i++) 
			{
				for (int j = 0; j < buttonNames.length; j++) 
				{
					cusHistoryView.addButtonListener(listeners.get(buttonNames[j]), buttonNames[j],i);
				}
			}
		}

}
